package com.lv.phuotbacnam.ui.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.gms.maps.CameraUpdate;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.OnMapClickListener;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import com.lv.phuotbacnam.R;
import com.lv.phuotbacnam.bl.log.Toaster;
import com.lv.phuotbacnam.ui.Constants;

public class GoogleMapChonViTriFragment extends BaseFragment {
	// Google Map
	private GoogleMap googleMap;
	/* Current marker */
	private Marker marker;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View rootView = inflater.inflate(
				R.layout.fragment_google_map_chon_vi_tri, container, false);

		// Loading map
		initilizeMap();

		return rootView;
	}

	/**
	 * function to load map. If map is not created it will create it for you
	 * */
	private void initilizeMap() {

		if (googleMap == null) {
			googleMap = ((MapFragment) getFragmentManager().findFragmentById(
					R.id.map)).getMap();

			// check if map is created successfully or not
			if (googleMap == null) {
				Toast.makeText(getActivity(), "Sorry! unable to create maps",
						Toast.LENGTH_SHORT).show();
			} else {
				LatLng latLng;
				float camera_zoom = Constants.CAMERA_ZOOM_DEFAULT;
				if (Constants.CURRENT_LOCATION == null)
					latLng = new LatLng(Constants.DEFAULT_LOCATION_LAT,
							Constants.DEFAULT_LOCATION_LONG);
				else {
					latLng = new LatLng(
							Constants.CURRENT_LOCATION.getLatitude(),
							Constants.CURRENT_LOCATION.getLongitude());
					camera_zoom = Constants.CAMERA_ZOOM_LOCATION_VISIBLE;

					/* Show icon of current location */
					showLocationIcon(latLng);

				}

				CameraUpdate center = CameraUpdateFactory.newLatLng(latLng);
				CameraUpdate zoom = CameraUpdateFactory.zoomTo(camera_zoom);

				googleMap.moveCamera(center);
				googleMap.animateCamera(zoom);
				googleMap.setOnMapClickListener(new OnMapClickListener() {

					@Override
					public void onMapClick(LatLng lat_lng) {
						Toaster.showToast(getActivity(), "Lat: "
								+ lat_lng.latitude + " - Lng: "
								+ lat_lng.longitude);

						CreateNewDiaDiem.vi_tri_da_chon = lat_lng;

						// remove old marker
						if (marker != null)
							marker.remove();

						// create marker
						MarkerOptions markerOptions = new MarkerOptions()
								.position(new LatLng(lat_lng.latitude,
										lat_lng.longitude));// .title("Hello Maps ");
						// GREEN color icon
						markerOptions.icon(BitmapDescriptorFactory
								.defaultMarker(BitmapDescriptorFactory.HUE_GREEN));
						// adding marker
						marker = googleMap.addMarker(markerOptions);
					}
				});
			}
		}
	}

	/**
	 * To show icon for current location
	 */
	private void showLocationIcon(LatLng currentLocation) {
		// create marker
		MarkerOptions markerOptions = new MarkerOptions().position(new LatLng(
				currentLocation.latitude, currentLocation.longitude));// .title("Hello Maps ");
		// Set icon
		markerOptions.icon(BitmapDescriptorFactory
				.fromResource(R.drawable.current_location));

		googleMap.addMarker(markerOptions);
	}

	@Override
	public void onResume() {
		super.onResume();
		//initilizeMap();
		//setRightAction();
	}

	@Override
	public void applyStyles() {
		super.applyStyles();
		getNavigationController().getNavigationBar().setActionTitle(
				getString(R.string.chon_vi_tri));

	}

	private void setRightAction() {
		TextView xong = new TextView(getActivity());
		xong.setText("Xong");
		getNavigationController().getNavigationBar().setRightAction(xong,
				new OnClickListener() {

					@Override
					public void onClick(View v) {
						getActivity().finish();
					}
				});
	}
}
